package com.dtruth.dataflow.mq;

import java.io.IOException;

import com.dtruth.dataflow.util.Env;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class RabbitMQProducer {

	private final ConnectionFactory connFactory;

	private Connection connection;
	private Channel channel;
	
	public static RabbitMQProducer instance = new RabbitMQProducer();

	private RabbitMQProducer() {
		connFactory = new ConnectionFactory();
		connFactory.setHost(Env.getInstance().getProperty("rabbitmq.host",
				"tree"));
		connFactory.setUsername("fenews");
		connFactory.setPassword("fenews");
		connFactory.setVirtualHost("fenews");

	}

	public void sendMessage(String message) {
		try {
			connection = connFactory.newConnection();
			channel = connection.createChannel();
			channel.queueDeclare("crawled", true, false, false, null);
			channel.basicPublish("", "crawled", null, message.getBytes());
			channel.close();
			connection.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}